public class BinarySearchTree {

    class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if(root == null){
            root = new TreeNode(key);
            return true;
        }

        TreeNode parent = root;
        TreeNode cur = root;
        while(cur != null){
            if(cur.key == key){
                return false;
            }else if(key < cur.key){
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }

        cur = new TreeNode(key);
        if(key < parent.key){
            parent.left = cur;
        }else{
            parent.right = cur;
        }

        return true;
    }


    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = root;
        while(cur != null){
            if(key == cur.key){
                return cur;
            }else if(key < cur.key){
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }

        return null;
    }


    //删除key的值
    public boolean remove(int key) {
        if(root == null){
            return false;
        }

        TreeNode parent = root;
        TreeNode cur = root;
        while(cur != null){
            if(key == cur.key){
                break;
            }else if(key < cur.key){
                parent = cur;
                cur = cur.left;
            }else{
                parent = cur;
                cur = cur.right;
            }
        }

        if(cur == null){
            return false;
        }

        if(cur.left == null && cur.right == null){
            if(cur == root){
                root = null;
            } else if(cur == parent.left){
                parent.left = null;
            } else{
                parent.right = null;
            }
            return true;
        }

        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            }else if(cur == parent.left){
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
            return true;
        }

        if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(cur == parent.left){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
            return true;
        }

        //在右子树中寻找最小(左)的节点，并与cur交换
        if(cur.left != null && cur.right != null){
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }

            cur.key = target.key;

            if(target == targetParent.right){
                targetParent.right = target.right;
            }else{
                targetParent.left = target.right;
            }
            return true;
        }
        return false;

    }


}
